package com.hibase.core.sequence.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.hibase.core.mybatis.service.BaseServiceImpl;
import com.hibase.core.sequence.entity.MdsCodeSequence;
import com.hibase.core.sequence.entity.vo.MdsCodeSequenceSearchVO;
import com.hibase.core.sequence.mapper.MdsCodeSequenceMapper;
import com.hibase.core.sequence.service.MdsCodeSequenceService;
import com.hibase.common.exception.HibaseAssert;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author hufeng
 * @since 2019-03-27
 */
@Service
@Slf4j
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class MdsCodeSequenceServiceImpl extends BaseServiceImpl<MdsCodeSequenceMapper, MdsCodeSequence> implements MdsCodeSequenceService {


    @Override
    public PageInfo<MdsCodeSequence> selectMdsCodeSequenceList(MdsCodeSequenceSearchVO searchVO) {
        QueryWrapper<MdsCodeSequence> queryWrapper = new QueryWrapper<>();

        if (StrUtil.isNotBlank(searchVO.getSequenceType())) {

            queryWrapper.eq("sequence_type", searchVO.getSequenceType());
        }

        if (StrUtil.isNotBlank(searchVO.getFix())) {

            queryWrapper.like("fix", "%" + searchVO.getFix().trim() + "%");
        }

        PageInfo<MdsCodeSequence> result = super.page(searchVO, queryWrapper);
        return result;
    }

    /**
     * 改变订单序列规则表记录
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveMdsCodeSequence(MdsCodeSequence entity) {

        super.store(entity);
    }

    /**
     * 查询订单序列规则表详情
     */
    @Override
    public MdsCodeSequence viewMdsCodeSequence(String id) {

        MdsCodeSequence entity = super.getById(id, true);

        return entity;
    }

    /**
     * 删除订单序列规则表记录
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteMdsCodeSequence(String id) {

        super.deleteBy(id);
    }

    /**
     * 启用禁用订单序列规则
     *
     * @param ids
     * @param status
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void changeMdsCodeSequence(List<String> ids, String status) {
        // step 1 查询订单序列规则数据
        Collection<MdsCodeSequence> items = super.listByIds(ids);
        HibaseAssert.isTrue(CollectionUtil.isNotEmpty(items), "service.mds.sequence.impl.mdsCodeSequenceServiceImpl.orderSequenceRuleNotFound");

        items.forEach(temp -> {
            // step 2 检查状态
            HibaseAssert.isFalse(status.equals(temp.getStatus()), "service.mds.sequence.impl.mdsCodeSequenceServiceImpl.doNotRepeat");
            // step 3 更新状态
            temp.setStatus(status);
        });

        super.storeBatch(items);
    }

}
